package com.home.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.home.school.bean.TOrder;
import com.home.school.bean.TSubject;
import com.home.school.bean.TTeacherSubject;
import com.home.school.bean.TUser;
import com.home.school.mapper.TSubjectMapper;
import com.home.school.service.TOrderService;
import com.home.school.service.TTeacherSubjectService;
import com.home.school.service.TUserService;
import com.home.school.vo.SubjectListVO;
import com.home.school.model.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.home.school.service.TSubjectService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class TSubjectServiceImpl extends ServiceImpl<TSubjectMapper, TSubject> implements TSubjectService{

    @Resource
    private TTeacherSubjectService tTeacherSubjectService;
    @Resource
    private TUserService tUserService;
    @Resource
    private TOrderService tOrderService;


    @Override
    public R pageList(SubjectListVO userListVO) {
        Page<TSubject> page = new Page<>(userListVO.getPageNum(), userListVO.getPageSize());
        IPage<TSubject> subjectIPage = baseMapper.selectPage(page, queryWrapper(userListVO));
        List<TSubject> pageRecords = subjectIPage.getRecords();
        for (TSubject pageRecord : pageRecords) {
            if(Objects.isNull(pageRecord)){
                continue;
            }
            String num = pageRecord.getNum();
            TTeacherSubject tTeacherSubject = tTeacherSubjectService.findBySubjectId(num);
            String teaId = tTeacherSubject.getTeaId();
            TUser tUser = tUserService.getById(teaId);
            pageRecord.setTeaId(String.valueOf(tUser.getId()));
            pageRecord.setUserName(tUser.getUsername());
            pageRecord.setAddress(tUser.getSchoolAddress());
            pageRecord.setPhone(tUser.getPhone());
        }
        return R.ok().data(subjectIPage);
    }

    @Override
    public TSubject findByNum(String num) {
        TSubject subject = new TSubject();
        subject.setNum(num);
        return baseMapper.selectOne(queryWrapper(subject));
    }

    @Override
    public R getTeacherList(SubjectListVO userListVO) {
        String teacherId = userListVO.getTeacherId();
        if(StringUtils.isNotEmpty(teacherId)){
            List<TTeacherSubject> tTeacherSubject =  tTeacherSubjectService.findByTeacherId(teacherId);
            if(CollectionUtils.isEmpty(tTeacherSubject)){
               return null;
            }
            List<String> collect = tTeacherSubject.stream().map(TTeacherSubject::getSujectId).collect(Collectors.toList());
            userListVO.setNum(collect);
        }
        Page<TSubject> page = new Page<>(userListVO.getPageNum(), userListVO.getPageSize());
        IPage<TSubject> subjectIPage = baseMapper.selectPage(page, queryWrapper(userListVO));
        List<TSubject> pageRecords = subjectIPage.getRecords();
        for (TSubject pageRecord : pageRecords) {
            if(Objects.isNull(pageRecord)){
                continue;
            }
            String num = pageRecord.getNum();
            TTeacherSubject tTeacherSubject = tTeacherSubjectService.findBySubjectId(num);
            String teaId = tTeacherSubject.getTeaId();
            TUser tUser = tUserService.getById(teaId);
            pageRecord.setUserName(tUser.getUsername());
        }
        return R.ok().data(subjectIPage);
    }

    public QueryWrapper<TSubject> queryWrapper(SubjectListVO subjectListVO){
        QueryWrapper<TSubject> tUserQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(subjectListVO.getTitle())){
            tUserQueryWrapper.eq("title", subjectListVO.getTitle());
        }

        if(StringUtils.isNotEmpty(subjectListVO.getType())){
            tUserQueryWrapper.eq("type", subjectListVO.getType());
        }
        if(!CollectionUtils.isEmpty(subjectListVO.getNum())){
            tUserQueryWrapper.in("num", subjectListVO.getNum());
        }

        return tUserQueryWrapper;
    }

    public QueryWrapper<TSubject> queryWrapper(TSubject subjectListVO){
        QueryWrapper<TSubject> tUserQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(subjectListVO.getTitle())){
            tUserQueryWrapper.eq("title", subjectListVO.getTitle());
        }
        if(StringUtils.isNotEmpty(subjectListVO.getNum())){
            tUserQueryWrapper.eq("num", subjectListVO.getNum());
        }
        return tUserQueryWrapper;
    }


}
